package com.sea.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sea.struct.data.InputShowData;
import com.sea.struct.data.InputShowData2;
import com.sea.struct.data.StatusData;
import com.sea.struct.data.StatusData2;
import com.sea.thread.MultiThreadScheduledService;
import com.sea.view.MainView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
@Component
public class MainController implements Initializable {

    @Autowired
    MainView mainView;


    private Stage stage;

    public Label UAB;

    public Label UBC;

    public Label UCA;

    public Label totalVoltage;

    public Label voltage1;

    public Label electri1;

    public Label faultCode1;

    public Label voltage2;

    public Label electri2;

    public Label faultCode2;

    public Label voltage3;

    public Label electri3;

    public Label faultCode3;

    public Label capacitySet;

    public Label lowerLimit1;

    public Label upperLimit1;

    public Label lowerLimit2;

    public Label upperLimit2;

    public Label lowerLimit3;

    public Label upperLimit3;

    public Label lowerLimit4;

    public Label upperLimit4;

    public Label lowerLimit5;

    public Label upperLimit5;

    public Label lowerLimit6;

    public Label upperLimit6;

    public Label lowerLimit7;

    public Label upperLimit7;

    public Label lowerLimit8;

    public Label upperLimit8;

    public Label lowerLimit9;

    public Label upperLimit9;

    public Label lowerLimit10;

    public Label upperLimit10;

    public Label startHourSet;

    public Label startMinSet;

    public Label endHourSet;

    public Label endMinSet;

    public Label lightBright;

    public Label lightTime;

    public Label timeRange;

    public Label driveIn01;

    public Label driveIn02;

    public Label driveIn03;

    public Label driveIn04;

    public Label driveIn05;

    public Label driveIn06;

    public Label driveIn07;

    public Label driveIn08;

    public Label driveIn09;

    public Label driveIn10;

    public Label driveIn11;

    public Label driveIn12;

    public Label driveIn13;

    public Label driveIn14;

    public Label driveIn15;

    public Label driveIn16;

    public Label driveIn17;

    public Label driveIn18;

    public Label driveIn19;

    public Label driveIn20;

    public Label driveIn21;

    public Label driveIn22;

    public Label driveIn23;

    public Label driveIn24;

    public Label acOverVoltageAlarm;

    public Label acUnderVoltageAlarm;

    public Label busOverVoltageAlarm;

    public Label busUnderVoltageAlarm;

    public Label insulationAnomalyAlarm;

    public Label overCurrentAlarm;

    public Label comInterruption;

    public Label UAB_2;

    public Label UBC_2;

    public Label UCA_2;

    public Label totalVoltage_2;

    public Label voltage1_2;

    public Label electri1_2;

    public Label faultCode1_2;

    public Label voltage2_2;

    public Label electri2_2;

    public Label faultCode2_2;

    public Label voltage3_2;

    public Label electri3_2;

    public Label faultCode3_2;

    public Label capacitySet_2;

    public Label lowerLimit1_2;

    public Label upperLimit1_2;

    public Label lowerLimit2_2;

    public Label upperLimit2_2;

    public Label lowerLimit3_2;

    public Label upperLimit3_2;

    public Label lowerLimit4_2;

    public Label upperLimit4_2;

    public Label lowerLimit5_2;

    public Label upperLimit5_2;

    public Label lowerLimit6_2;

    public Label upperLimit6_2;

    public Label lowerLimit7_2;

    public Label upperLimit7_2;

    public Label lowerLimit8_2;

    public Label upperLimit8_2;

    public Label lowerLimit9_2;

    public Label upperLimit9_2;

    public Label lowerLimit10_2;

    public Label upperLimit10_2;

    public Label startHourSet_2;

    public Label startMinSet_2;

    public Label endHourSet_2;

    public Label endMinSet_2;

    public Label lightBright_2;

    public Label lightTime_2;

    public Label timeRange_2;

    public Label driveIn01_2;

    public Label driveIn02_2;

    public Label driveIn03_2;

    public Label driveIn04_2;

    public Label driveIn05_2;

    public Label driveIn06_2;

    public Label driveIn07_2;

    public Label driveIn08_2;

    public Label driveIn09_2;

    public Label driveIn10_2;

    public Label driveIn11_2;

    public Label driveIn12_2;

    public Label driveIn13_2;

    public Label driveIn14_2;

    public Label driveIn15_2;

    public Label driveIn16_2;

    public Label driveIn17_2;

    public Label driveIn18_2;

    public Label driveIn19_2;

    public Label driveIn20_2;

    public Label driveIn21_2;

    public Label driveIn22_2;

    public Label driveIn23_2;

    public Label driveIn24_2;

    public Label acOverVoltageAlarm_2;

    public Label acUnderVoltageAlarm_2;

    public Label busOverVoltageAlarm_2;

    public Label busUnderVoltageAlarm_2;

    public Label insulationAnomalyAlarm_2;

    public Label overCurrentAlarm_2;

    public Label comInterruption_2;

    public TextField port;

    public void openPort() throws IOException {

        String text = port.getText();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(new File("port.json"), text);

        MultiThreadScheduledService multiThreadScheduledService = new MultiThreadScheduledService("ScheduledService");
        //上次运行开始和下一次运行开始之间允许的最短时间。
        multiThreadScheduledService.setPeriod(Duration.millis(5000));
        //ScheduledService 首次启动与开始运行之间的初始延迟。
        multiThreadScheduledService.setDelay(Duration.millis(5000));

        //设置 ScheduledService 是否应在 Task 失败的情况下自动重新启动。
        multiThreadScheduledService.setRestartOnFailure(false);
        //ScheduledService 在简单地以 FAILED 状态结束之前可以失败的最大次数。
        //multiThreadScheduledService.setMaximumFailureCount(10);

        //给ScheduledService添加Task执行成功事件
        multiThreadScheduledService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {

                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    InputShowData showData = objectMapper.readValue(new File("host_1_3.json"), InputShowData.class);
                    UAB.setText(String.valueOf(showData.getUAB()));
                    UBC.setText(String.valueOf(showData.getUBC()));
                    UCA.setText(String.valueOf(showData.getUCA()));
                    totalVoltage.setText(String.valueOf(showData.getTotalVoltage()));
                    voltage1.setText(String.valueOf(showData.getVoltage1()));
                    electri1.setText(String.valueOf(showData.getElectri1()));
                    faultCode1.setText(String.valueOf(showData.getFaultCode1()));
                    voltage2.setText(String.valueOf(showData.getVoltage2()));
                    electri2.setText(String.valueOf(showData.getElectri2()));
                    faultCode2.setText(String.valueOf(showData.getFaultCode2()));
                    voltage3.setText(String.valueOf(showData.getVoltage3()));
                    electri3.setText(String.valueOf(showData.getElectri3()));
                    faultCode3.setText(String.valueOf(showData.getFaultCode3()));
                    startHourSet.setText(String.valueOf(showData.getStartHourSet()));
                    startMinSet.setText(String.valueOf(showData.getStartMinSet()));
                    endHourSet.setText(String.valueOf(showData.getEndHourSet()));
                    endMinSet.setText(String.valueOf(showData.getEndMinSet()));
                    capacitySet.setText(String.valueOf(showData.getCapacitySet()));
                    lowerLimit1.setText(String.valueOf(showData.getLowerLimit1()));
                    upperLimit1.setText(String.valueOf(showData.getUpperLimit1()));
                    lowerLimit2.setText(String.valueOf(showData.getLowerLimit2()));
                    upperLimit2.setText(String.valueOf(showData.getUpperLimit2()));
                    lowerLimit3.setText(String.valueOf(showData.getLowerLimit3()));
                    upperLimit3.setText(String.valueOf(showData.getUpperLimit3()));
                    lowerLimit4.setText(String.valueOf(showData.getLowerLimit4()));
                    upperLimit4.setText(String.valueOf(showData.getUpperLimit4()));
                    lowerLimit5.setText(String.valueOf(showData.getLowerLimit5()));
                    upperLimit5.setText(String.valueOf(showData.getUpperLimit5()));
                    lowerLimit6.setText(String.valueOf(showData.getLowerLimit6()));
                    upperLimit6.setText(String.valueOf(showData.getUpperLimit6()));
                    lowerLimit7.setText(String.valueOf(showData.getLowerLimit7()));
                    upperLimit7.setText(String.valueOf(showData.getUpperLimit7()));
                    lowerLimit8.setText(String.valueOf(showData.getLowerLimit8()));
                    upperLimit8.setText(String.valueOf(showData.getUpperLimit8()));
                    lowerLimit9.setText(String.valueOf(showData.getLowerLimit9()));
                    upperLimit9.setText(String.valueOf(showData.getUpperLimit9()));
                    lowerLimit10.setText(String.valueOf(showData.getLowerLimit10()));
                    upperLimit10.setText(String.valueOf(showData.getUpperLimit10()));
                    lightBright.setText(String.valueOf(showData.getLightBright()));
                    lightTime.setText(String.valueOf(showData.getLightTime()));
                    timeRange.setText(String.valueOf(showData.getTimeRange()));

                    StatusData statusData = objectMapper.readValue(new File("host_1_2.json"), StatusData.class);
                    driveIn01.setText(statusData.getDriveIn01().equals("0") ? "无" : "有");
                    driveIn02.setText(statusData.getDriveIn02().equals("0") ? "无" : "有");
                    driveIn03.setText(statusData.getDriveIn03().equals("0") ? "无" : "有");
                    driveIn04.setText(statusData.getDriveIn04().equals("0") ? "无" : "有");
                    driveIn05.setText(statusData.getDriveIn05().equals("0") ? "无" : "有");
                    driveIn06.setText(statusData.getDriveIn06().equals("0") ? "无" : "有");

                    driveIn07.setText(statusData.getDriveIn07().equals("0") ? "无" : "有");
                    driveIn08.setText(statusData.getDriveIn08().equals("0") ? "无" : "有");
                    driveIn09.setText(statusData.getDriveIn09().equals("0") ? "无" : "有");
                    driveIn10.setText(statusData.getDriveIn10().equals("0") ? "无" : "有");
                    driveIn11.setText(statusData.getDriveIn11().equals("0") ? "无" : "有");
                    driveIn12.setText(statusData.getDriveIn12().equals("0") ? "无" : "有");

                    driveIn13.setText(statusData.getDriveIn13().equals("0") ? "无" : "有");
                    driveIn14.setText(statusData.getDriveIn14().equals("0") ? "无" : "有");
                    driveIn15.setText(statusData.getDriveIn15().equals("0") ? "无" : "有");
                    driveIn16.setText(statusData.getDriveIn16().equals("0") ? "无" : "有");
                    driveIn17.setText(statusData.getDriveIn17().equals("0") ? "无" : "有");
                    driveIn18.setText(statusData.getDriveIn18().equals("0") ? "无" : "有");

                    driveIn19.setText(statusData.getDriveIn19().equals("0") ? "无" : "有");
                    driveIn20.setText(statusData.getDriveIn20().equals("0") ? "无" : "有");
                    driveIn21.setText(statusData.getDriveIn21().equals("0") ? "无" : "有");
                    driveIn22.setText(statusData.getDriveIn22().equals("0") ? "无" : "有");
                    driveIn23.setText(statusData.getDriveIn23().equals("0") ? "无" : "有");
                    driveIn24.setText(statusData.getDriveIn24().equals("0") ? "无" : "有");

                    acOverVoltageAlarm.setText(statusData.getAcOverVoltageAlarm().equals("0") ? "无" : "告警");
                    acUnderVoltageAlarm.setText(statusData.getAcUnderVoltageAlarm().equals("0") ? "无" : "告警");
                    busOverVoltageAlarm.setText(statusData.getBusOverVoltageAlarm().equals("0") ? "无" : "告警");
                    busUnderVoltageAlarm.setText(statusData.getBusUnderVoltageAlarm().equals("0") ? "无" : "告警");
                    insulationAnomalyAlarm.setText(statusData.getInsulationAnomalyAlarm().equals("0") ? "无" : "告警");
                    overCurrentAlarm.setText(statusData.getOverCurrentAlarm().equals("0") ? "无" : "告警");
                    comInterruption.setText(statusData.getComInterruption().equals("0") ? "无" : "告警");


                    InputShowData2 showData2 = objectMapper.readValue(new File("host_2_3.json"), InputShowData2.class);

                    UAB_2.setText(String.valueOf(showData2.getUAB()));
                    UBC_2.setText(String.valueOf(showData2.getUBC()));
                    UCA_2.setText(String.valueOf(showData2.getUCA()));
                    totalVoltage_2.setText(String.valueOf(showData2.getTotalVoltage()));
                    voltage1_2.setText(String.valueOf(showData2.getVoltage1()));
                    electri1_2.setText(String.valueOf(showData2.getElectri1()));
                    faultCode1_2.setText(String.valueOf(showData2.getFaultCode1()));
                    voltage2_2.setText(String.valueOf(showData2.getVoltage2()));
                    electri2_2.setText(String.valueOf(showData2.getElectri2()));
                    faultCode2_2.setText(String.valueOf(showData2.getFaultCode2()));
                    voltage3_2.setText(String.valueOf(showData2.getVoltage3()));
                    electri3_2.setText(String.valueOf(showData2.getElectri3()));
                    faultCode3_2.setText(String.valueOf(showData2.getFaultCode3()));
                    startHourSet_2.setText(String.valueOf(showData2.getStartHourSet()));
                    startMinSet_2.setText(String.valueOf(showData2.getStartMinSet()));
                    endHourSet_2.setText(String.valueOf(showData2.getEndHourSet()));
                    endMinSet_2.setText(String.valueOf(showData2.getEndMinSet()));
                    capacitySet_2.setText(String.valueOf(showData2.getCapacitySet()));
                    lowerLimit1_2.setText(String.valueOf(showData2.getLowerLimit1()));
                    upperLimit1_2.setText(String.valueOf(showData2.getUpperLimit1()));
                    lowerLimit2_2.setText(String.valueOf(showData2.getLowerLimit2()));
                    upperLimit2_2.setText(String.valueOf(showData2.getUpperLimit2()));
                    lowerLimit3_2.setText(String.valueOf(showData2.getLowerLimit3()));
                    upperLimit3_2.setText(String.valueOf(showData2.getUpperLimit3()));
                    lowerLimit4_2.setText(String.valueOf(showData2.getLowerLimit4()));
                    upperLimit4_2.setText(String.valueOf(showData2.getUpperLimit4()));
                    lowerLimit5_2.setText(String.valueOf(showData2.getLowerLimit5()));
                    upperLimit5_2.setText(String.valueOf(showData2.getUpperLimit5()));
                    lowerLimit6_2.setText(String.valueOf(showData2.getLowerLimit6()));
                    upperLimit6_2.setText(String.valueOf(showData2.getUpperLimit6()));
                    lowerLimit7_2.setText(String.valueOf(showData2.getLowerLimit7()));
                    upperLimit7_2.setText(String.valueOf(showData2.getUpperLimit7()));
                    lowerLimit8_2.setText(String.valueOf(showData2.getLowerLimit8()));
                    upperLimit8_2.setText(String.valueOf(showData2.getUpperLimit8()));
                    lowerLimit9_2.setText(String.valueOf(showData2.getLowerLimit9()));
                    upperLimit9_2.setText(String.valueOf(showData2.getUpperLimit9()));
                    lowerLimit10_2.setText(String.valueOf(showData2.getLowerLimit10()));
                    upperLimit10_2.setText(String.valueOf(showData2.getUpperLimit10()));
                    lightBright_2.setText(String.valueOf(showData2.getLightBright()));
                    lightTime_2.setText(String.valueOf(showData2.getLightTime()));
                    timeRange_2.setText(String.valueOf(showData2.getTimeRange()));

                    StatusData2 statusData2 = objectMapper.readValue(new File("host_2_2.json"), StatusData2.class);
                    driveIn01_2.setText(statusData2.getDriveIn01().equals("0") ? "无" : "有");
                    driveIn02_2.setText(statusData2.getDriveIn02().equals("0") ? "无" : "有");
                    driveIn03_2.setText(statusData2.getDriveIn03().equals("0") ? "无" : "有");
                    driveIn04_2.setText(statusData2.getDriveIn04().equals("0") ? "无" : "有");
                    driveIn05_2.setText(statusData2.getDriveIn05().equals("0") ? "无" : "有");
                    driveIn06_2.setText(statusData2.getDriveIn06().equals("0") ? "无" : "有");

                    driveIn07_2.setText(statusData2.getDriveIn07().equals("0") ? "无" : "有");
                    driveIn08_2.setText(statusData2.getDriveIn08().equals("0") ? "无" : "有");
                    driveIn09_2.setText(statusData2.getDriveIn09().equals("0") ? "无" : "有");
                    driveIn10_2.setText(statusData2.getDriveIn10().equals("0") ? "无" : "有");
                    driveIn11_2.setText(statusData2.getDriveIn11().equals("0") ? "无" : "有");
                    driveIn12_2.setText(statusData2.getDriveIn12().equals("0") ? "无" : "有");

                    driveIn13_2.setText(statusData2.getDriveIn13().equals("0") ? "无" : "有");
                    driveIn14_2.setText(statusData2.getDriveIn14().equals("0") ? "无" : "有");
                    driveIn15_2.setText(statusData2.getDriveIn15().equals("0") ? "无" : "有");
                    driveIn16_2.setText(statusData2.getDriveIn16().equals("0") ? "无" : "有");
                    driveIn17_2.setText(statusData2.getDriveIn17().equals("0") ? "无" : "有");
                    driveIn18_2.setText(statusData2.getDriveIn18().equals("0") ? "无" : "有");

                    driveIn19_2.setText(statusData2.getDriveIn19().equals("0") ? "无" : "有");
                    driveIn20_2.setText(statusData2.getDriveIn20().equals("0") ? "无" : "有");
                    driveIn21_2.setText(statusData2.getDriveIn21().equals("0") ? "无" : "有");
                    driveIn22_2.setText(statusData2.getDriveIn22().equals("0") ? "无" : "有");
                    driveIn23_2.setText(statusData2.getDriveIn23().equals("0") ? "无" : "有");
                    driveIn24_2.setText(statusData2.getDriveIn24().equals("0") ? "无" : "有");

                    acOverVoltageAlarm_2.setText(statusData2.getAcOverVoltageAlarm().equals("0") ? "无" : "告警");
                    acUnderVoltageAlarm_2.setText(statusData2.getAcUnderVoltageAlarm().equals("0") ? "无" : "告警");
                    busOverVoltageAlarm_2.setText(statusData2.getBusOverVoltageAlarm().equals("0") ? "无" : "告警");
                    busUnderVoltageAlarm_2.setText(statusData2.getBusUnderVoltageAlarm().equals("0") ? "无" : "告警");
                    insulationAnomalyAlarm_2.setText(statusData2.getInsulationAnomalyAlarm().equals("0") ? "无" : "告警");
                    overCurrentAlarm_2.setText(statusData2.getOverCurrentAlarm().equals("0") ? "无" : "告警");
                    comInterruption_2.setText(statusData2.getComInterruption().equals("0") ? "无" : "告警");
                } catch (IOException e) {
                    System.out.println("JSON文件读取失败：" + e.getMessage());
                }
            }
        });
        multiThreadScheduledService.start();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        init();
    }

    void init() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Parent parent = mainView.getView();
                MainController.this.stage = (Stage) parent.getScene().getWindow();
                stage.setTitle("电源监控V1.0  ---明瑞智能(www.ahmrzn.com)");
                stage.setResizable(false);
            }
        });
    }
}
